package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.JsonUtil;
import com.coolbi.onsupai.core.biz.SettleCategoryBiz;
import com.coolbi.onsupai.core.entity.SettleCategory;
import com.coolbi.security.web.action.SessionAction;

/**
 * @Description SettleCategory Action 结算方式Action
 * @author for2cold
 * @codeDate 2012-7-9 下午03:25:24
 * 
 */
public class SettleCategoryAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private SettleCategory settleCategory;
	private List<SettleCategory> settleCategoryList;
	@Autowired
	private SettleCategoryBiz settleCategoryBiz;

	/**
	 * 添加结算方式
	 * 
	 * @return
	 */
	public String save() {
		try {
			settleCategoryBiz.save(settleCategory);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 分页查询所有结算方式
	 * 
	 * @return
	 */
	public String listByPage() throws Exception {
		pager.setRecordCount(settleCategoryBiz.count());
		settleCategoryList = settleCategoryBiz.listByPage(
				(pager.getCurrPageNo() - 1) * pager.getPageSize(),
				pager.getPageSize());
		return SUCCESS;
	}

	/**
	 * 根据ID查看结算方式
	 * 
	 * @return
	 */
	public String findById() {
		try {
			settleCategory = settleCategoryBiz.get(settleCategory.getId());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 更新结算方式
	 * 
	 * @return
	 */
	public String update() {
		try {
			settleCategoryBiz.update(settleCategory);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 删除结算方式
	 * 
	 * @return
	 */
	public String delete() {
		try {
			settleCategoryBiz.delete(settleCategory.getId());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 获取所有结算方式,返回Json数据
	 * @return
	 */
	public String findAllSettleCategory() {
		String json = null;
		try {
			settleCategoryList = settleCategoryBiz.list();
			String[] fields = {"settleName", "id"};
			json = JsonUtil.toJsonByArray(settleCategoryList, fields);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ajax(json);
	}

	public SettleCategory getSettleCategory() {
		return settleCategory;
	}

	public void setSettleCategory(SettleCategory settleCategory) {
		this.settleCategory = settleCategory;
	}

	public List<SettleCategory> getSettleCategoryList() {
		return settleCategoryList;
	}

	public void setSettleCategoryList(List<SettleCategory> settleCategoryList) {
		this.settleCategoryList = settleCategoryList;
	}

	public void setSettleCategoryBiz(SettleCategoryBiz settleCategoryBiz) {
		this.settleCategoryBiz = settleCategoryBiz;
	}
}
